<script setup lang="tsx">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { useI18n } from '@/hooks/web/useI18n'
import { Table } from '@/components/Table'
import { useTable } from '@/hooks/web/useTable'
import { ref, unref, reactive, watch, computed } from 'vue'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { usePatrolGroupStore } from '@/store/modules/PlanGroupStore'
import PlanGroupApi from '@/api/linePatrol/PlanGroupApi'
import { useDictStore } from '@/store/modules/dictData'
import { PatrolInspectionType } from '@/api/linePatrol/type'
import { useDeptStore } from '@/store/modules/dept'
const DeptStore = useDeptStore()
const GroupStore = usePatrolGroupStore()
if (DeptStore.DeptListTypeDetail.length == 0) {
  DeptStore.setFacilityTypeDetail()
}
const ids = ref<string[]>([])
const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const { currentPage, pageSize } = tableState
    const res = await PlanGroupApi.GetTaskIdList({
      pageNum: unref(currentPage),
      pageSize: unref(pageSize),
      taskId: GroupStore.GroupDataID,
      ...unref(searchParams)
    })
    return {
      list: res.rows,
      total: res.total
    }
  },
  fetchDelApi: async () => {
    const params = {
      taskId: GroupStore.GroupDataID,
      uuid: unref(ids)[0]
    }
    const res = await PlanGroupApi.DelTaskData(params)
    return !!res
  }
})
const { loading, dataList, total, currentPage, pageSize } = tableState
const { getList, getElTableExpose, delList } = tableMethods
// fac_dict_type
const DictStore = useDictStore()
//获取字典
DictStore.setDict('fac_dict_type') //设施字典类型
const searchParams = ref({})
const setSearchParams = (params: any) => {
  searchParams.value = params
  getList()
}
const recorders = computed(() => GroupStore.GroupDataID)
watch(recorders, () => {
  getList()
})
//删除
const delLoading = ref(false)
const delData = async (row: PatrolInspectionType | null) => {
  const elTableExpose = await getElTableExpose()
  ids.value = row
    ? [row.uuid]
    : elTableExpose?.getSelectionRows().map((v: PatrolInspectionType) => v.uuid) || []
  delLoading.value = true
  await delList(unref(ids).length).finally(() => {
    delLoading.value = false
  })
}
const { t } = useI18n()

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'index',
    label: t('tableDemo.index'),
    type: 'index',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    }
  },
  {
    field: 'facName',
    label: t('巡检点名称'),
    search: {
      component: 'Input',
      componentProps: {
        placeholder: '巡检点名称'
      }
    },
    form: {
      component: 'Input',
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24
    }
  },
  {
    field: 'qrCode',
    label: t('二维码'),
    search: {
      hidden: true
    }
  },
  {
    field: 'facDictType', //设施字典类型
    label: t('类型'),
    search: {
      hidden: true
    },
    formatter: (_: Recordable, __: any, cellValue: string) => {
      return DictStore.$state.fac_dict_type?.map((item) => {
        if (item.dictValue == cellValue) {
          return t(item.dictLabel)
        }
      })
    }
  },
  {
    field: 'coId',
    label: t('公司'),
    search: {
      hidden: true
    },
    width: 120,
    formatter: (_: Recordable, __: any, cellValue: number) => {
      if (cellValue !== undefined && cellValue) {
        return DeptStore.getDeptName(cellValue)[0]?.deptName
      }
    }
  },
  {
    field: 'pageviews',
    label: t('巡线片区'), //暂无
    search: {
      hidden: true
    }
  },
  {
    field: 'userAreaId', //用户片区ID
    label: t('用户片区'),
    search: {
      hidden: true
    },
    table: {
      show: false
    },
    form: {
      component: 'Editor',
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24,
      slots: {
        default: (data: any) => {
          return <div innerHTML={data.content}></div>
        }
      }
    }
  },
  {
    field: 'surveyLocUuid',
    label: t('关联地图'),
    search: {
      hidden: true
    },
    table: {
      slots: {
        default: (data: any) => {
          if (data.row?.surveyLocUuid) {
            return (
              <>
                <span>是</span>
              </>
            )
          } else {
            return (
              <>
                <span>否</span>
              </>
            )
          }
        }
      }
    }
  },
  {
    field: 'action',
    label: t('tableDemo.action'),
    width: 100,
    fixed: 'right',
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    detail: {
      hidden: true
    },
    table: {
      slots: {
        default: (data: any) => {
          return (
            <>
              <span class="TableBtn" style="color:#f56c6c" onClick={() => delData(data.row)}>
                {t('删除')}
              </span>
            </>
          )
        }
      }
    }
  }
])

// @ts-ignore
const { allSchemas } = useCrudSchemas(crudSchemas)
</script>

<template>
  <ContentWrap :title="t('巡线列表')">
    <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />

    <Table
      v-model:pageSize="pageSize"
      v-model:currentPage="currentPage"
      :highlight-current-row="true"
      :columns="allSchemas.tableColumns"
      :data="dataList"
      :loading="loading"
      :pagination="{
        total: total,
        pageSize: pageSize
      }"
      @register="tableRegister"
    />
  </ContentWrap>
</template>
<style scoped>
:deep(.el-table--default .el-table__cell) {
  padding: 6px 0;
}
.TableBtn {
  cursor: pointer;
  font-weight: 600;
  margin: 0 5px;
}
</style>
@/store/modules/PlanGroupApi@/api/linePatrol/PlanGroupApi
